package com.java110.mall.cart.bmo.housekeepingServPool.impl;


import com.java110.mall.cart.bmo.housekeepingServPool.IGetHousekeepingServPoolBMO;
import com.java110.dto.file.FileRelDto;
import com.java110.dto.housekeeping.HousekeepingServDto;
import com.java110.dto.housekeeping.HousekeepingServUserDto;
import com.java110.intf.cart.IHousekeepingServPoolInnerServiceSMO;
import com.java110.intf.cart.IHousekeepingServUserInnerServiceSMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.intf.goods.IHousekeepingServInnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.java110.dto.housekeeping.HousekeepingServPoolDto;


import java.util.ArrayList;
import java.util.List;

@Service("getHousekeepingServPoolBMOImpl")
public class GetHousekeepingServPoolBMOImpl implements IGetHousekeepingServPoolBMO {

    @Autowired
    private IHousekeepingServPoolInnerServiceSMO housekeepingServPoolInnerServiceSMOImpl;
    @Autowired
    private IHousekeepingServUserInnerServiceSMO housekeepingServUserInnerServiceSMOImpl;
    @Autowired
    private IHousekeepingServInnerServiceSMO housekeepingServInnerServiceSMOImpl;
    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;
    /**
     * @param housekeepingServPoolDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(HousekeepingServPoolDto housekeepingServPoolDto) {


        int count = housekeepingServPoolInnerServiceSMOImpl.queryHousekeepingServPoolsCount(housekeepingServPoolDto);

        List<HousekeepingServPoolDto> housekeepingServPoolDtos = null;
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        if (count > 0) {
            housekeepingServPoolDtos = housekeepingServPoolInnerServiceSMOImpl.queryHousekeepingServPools(housekeepingServPoolDto);
            if (null != housekeepingServPoolDtos && housekeepingServPoolDtos.size() > 0) {
                for (HousekeepingServPoolDto ServPoolDto : housekeepingServPoolDtos) {
                    HousekeepingServDto housekeepingServDto = new HousekeepingServDto();
                    housekeepingServDto.setServId(ServPoolDto.getServId());
                    housekeepingServDto.setShopId(ServPoolDto.getShopId());
                    /**查询工单所挂的服务信息，目前场景一个工单对应一个服务，一个服务可以有多个工单，但目前的业务逻辑是工单
                     *  向上查服务，因此只有一个工单对应一个服务的业务，因此下面循环可以不使用，有数据时直接获取数组 0 位置的值
                     *  直接进行查询服务所关联的师傅的信息。但为了防止后期有其他场景暂时先用循环
                     */
                    List<HousekeepingServDto> housekeepingServDtos = housekeepingServInnerServiceSMOImpl.queryHousekeepingServs(housekeepingServDto);
                    ServPoolDto.setHousekeepingServDtos(housekeepingServDtos);
                    if (null != housekeepingServDtos && housekeepingServDtos.size() > 0) {
                        /**查询服务所关联的维修师傅的信息**/
                        for (HousekeepingServDto servDto : housekeepingServDtos) {
                            HousekeepingServUserDto housekeepingServUserDto = new HousekeepingServUserDto();
                            housekeepingServUserDto.setServId(servDto.getServId());
                            housekeepingServUserDto.setShopId(servDto.getShopId());
                            List<HousekeepingServUserDto> housekeepingServUserDtos = housekeepingServUserInnerServiceSMOImpl.queryHousekeepingServUsers(housekeepingServUserDto);
                            //封面图
                            servDto.setCoverPhoto(imgUrl + doGetRentingFile(servDto, FileRelDto.REL_TYPE_CD_GOODS_COVER));
                            servDto.setHousekeepingServUserDtos(housekeepingServUserDtos);
                        }
                    }
                }
            }
        } else {
            housekeepingServPoolDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) housekeepingServPoolDto.getRow()), count, housekeepingServPoolDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    private String doGetRentingFile(HousekeepingServDto housekeepingServDto, String fileTypeCd) {

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjId(housekeepingServDto.getServId());
        fileRelDto.setRelTypeCd(fileTypeCd);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);
        FileRelDto fileRel = null;
        if (null != fileRelDtos && fileRelDtos.size() > 0) {
            fileRel = fileRelDtos.get(0);
            return fileRel.getFileSaveName();
        }
        return "";
    }
    @Override
    public ResponseEntity<String> queryStaffServ(HousekeepingServPoolDto housekeepingServPoolDto) {
        int count = housekeepingServPoolInnerServiceSMOImpl.queryStaffServCount(housekeepingServPoolDto);

        List<HousekeepingServPoolDto> housekeepingServPoolDtos = null;
        if (count > 0) {
            housekeepingServPoolDtos = housekeepingServPoolInnerServiceSMOImpl.queryStaffServs(housekeepingServPoolDto);
        } else {
            housekeepingServPoolDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) housekeepingServPoolDto.getRow()), count, housekeepingServPoolDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    @Override
    public ResponseEntity<String> queryStaffFinishServ(HousekeepingServPoolDto housekeepingServPoolDto) {
        int count = housekeepingServPoolInnerServiceSMOImpl.queryStaffFinishServCount(housekeepingServPoolDto);

        List<HousekeepingServPoolDto> housekeepingServPoolDtos = null;
        if (count > 0) {
            housekeepingServPoolDtos = housekeepingServPoolInnerServiceSMOImpl.queryStaffFinishServs(housekeepingServPoolDto);
        } else {
            housekeepingServPoolDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) housekeepingServPoolDto.getRow()), count, housekeepingServPoolDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

}
